﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using AppBlank.Models;

namespace AppBlank.Services
{
    public class ListViewDemoService : IDataServices
    {
        public List<MeiZiTuFuLi> GetMeiZiTuFuLi()
        {
            string strUrl = "http://gank.io/api/random/data/%E7%A6%8F%E5%88%A9/20";
            try
            {
                string result = string.Empty;
                HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                httpRequest.Timeout = 10000;
                httpRequest.UserAgent = "Code Sample Web Client";
                httpRequest.Credentials = CredentialCache.DefaultCredentials;
                HttpWebResponse respone = (HttpWebResponse)httpRequest.GetResponse();
                StreamReader stream = new StreamReader(respone.GetResponseStream(), Encoding.UTF8);
                result = stream.ReadToEnd();
                stream.Close();
                Console.WriteLine(result);
                return Newtonsoft.Json.JsonConvert.DeserializeObject<MeiZiTuFuLiMain>(result).results;
            }
            catch (Exception ee)
            {

                return new List<MeiZiTuFuLi>();
            }



        }

        public List<KuaiYinShiVideo> GetVideoList(int page)
        {
          string strUrl = string.Format("https://kuaiyinshi.com/api/hot/videos/?source=kuai-shou&page={0}&st=week", page);
            //  string strUrl = string.Format("https://kuaiyinshi.com/api/hot/videos/?source=mei-pai&page={0}&st=week", page);
            try
            {
                string result = string.Empty;
                HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                httpRequest.Timeout = 10000;
                httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36";
                //httpRequest.Headers.Add("Accept", "*/*");
                // ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Ssl3;
                httpRequest.Credentials = CredentialCache.DefaultCredentials;
                HttpWebResponse respone = (HttpWebResponse)httpRequest.GetResponse();
                StreamReader stream = new StreamReader(respone.GetResponseStream(), Encoding.UTF8);
                result = stream.ReadToEnd();
                stream.Close();
                //result = NoHTML(Regex.Unescape(result));
                Console.WriteLine(result);
                var list = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.KuaiYinShi>(result);
                return list.data;

            }
            catch (Exception ee)
            {
                return new List<KuaiYinShiVideo>();
            }

        }


        /// <summary>
        /// Json转换成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonText"></param>
        /// <returns></returns>
        public static T JsonToObject<T>(string jsonText)
        {
            // Framework 2.0 不支持
            DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonText));
            T obj = (T)s.ReadObject(ms);
            ms.Dispose();
            return obj;


        }


        public static string NoHTML(string Htmlstring)
        {
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);

            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);

            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            // Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

            return Htmlstring;
        }


    }
}
